package com.wing.sell.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.sell.dao.FinishedOrderDao;


import com.wing.sell.model.entity.FinishedOrder;


import com.wing.sell.model.entity.FinishedOrderDetail;
import com.wing.sell.model.request.FinishedOrderForm;
import com.wing.sell.model.response.FinishedOrderVo;
import com.wing.sell.model.response.OrderVO;
import com.wing.sell.service.FinishedOrderDetailService;
import com.wing.sell.service.FinishedOrderService;

import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 销售订单表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class FinishedOrderServiceImpl extends ServiceImpl<FinishedOrderDao, FinishedOrder> implements FinishedOrderService {

    @Autowired
    private FinishedOrderDetailService finishedOrderDetailService;

    /**
     * 查询成品销售订单
     * @param pg
     * @param params
     * @return
     */
    @Override
    public Page<FinishedOrderVo> pageList4app(Page<FinishedOrderVo> pg, Map params) {
        return baseMapper.pageList4app(pg,params);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public FinishedOrderVo getDetailBy(Long id) {
        return baseMapper.getDetailBy(id);
    }

    /**
     * 添加成品单价
     * @param finishedOrderForm
     * @return
     */
    @Override
    @Transactional
    public boolean addInfo(FinishedOrderForm finishedOrderForm) {
        FinishedOrder finishedOrder = new FinishedOrder();
        BeanUtil.copyProperties(finishedOrderForm,finishedOrder,true);
        finishedOrder.setCreateTime(LocalDateTime.now());
        finishedOrder.setCreator(JwtUtils.getLoginUserId());
        finishedOrder.setCreatorName(JwtUtils.getLoginUsername());
        finishedOrder.setSelStatus(ProductEnum.finishendOrderStatusEmum.DTJ.getCode());
        finishedOrder.setModifier(JwtUtils.getLoginUserId());
        finishedOrder.setPcStatus(MaintenanceEnum.pcStatusEmum.DPC.getCode());
        finishedOrder.setSkuQuantity(finishedOrderForm.getDetailList().size());
        String taskNo = UniqueIdGeneratorUtil.generateUniqueId("CPD", "yyyyMMdd", 1000, 9999);
        finishedOrder.setSelCode(taskNo);
        BigDecimal sum = finishedOrderForm.getDetailList().stream().map(FinishedOrderDetail::getTotalMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        finishedOrder.setTotalMoney(sum);
        boolean save = this.save(finishedOrder);
        if (save){
            List<FinishedOrderDetail> detailList = finishedOrderForm.getDetailList();
            detailList.forEach(info->{
                info.setCreateTime(LocalDateTime.now());
                info.setCreator(JwtUtils.getLoginUserId());
                info.setCreatorName(JwtUtils.getLoginUsername());
                info.setModifier(JwtUtils.getLoginUserId());
                info.setFinishedId(finishedOrder.getId());
                info.setSelRemainQuality(info.getSelQuality());
            });
            boolean saveBatch = finishedOrderDetailService.saveBatch(detailList);
            if (saveBatch){
                return true;
            }
        }
        return false;
    }


    /**
     * 修改
     * @param finishedOrderForm
     * @return
     */
    @Override
    @Transactional
    public boolean updateInfo(FinishedOrderForm finishedOrderForm) {
        FinishedOrder byId = this.getById(finishedOrderForm.getId());
                                       BeanUtil.copyProperties(finishedOrderForm,byId,true);
        byId.setUpdateTime(LocalDateTime.now());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        BigDecimal sum = finishedOrderForm.getDetailList().stream().map(FinishedOrderDetail::getTotalMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        byId.setSkuQuantity(finishedOrderForm.getDetailList().size());
        byId.setTotalMoney(sum);
        boolean b = this.updateById(byId);
        if (b){
            List<FinishedOrderDetail> detailList = finishedOrderForm.getDetailList();
            if (CollectionUtil.isNotEmpty(detailList)) {
                detailList.forEach(info->{
                    info.setSelRemainQuality(info.getSelQuality());
                    info.setUpdateTime(LocalDateTime.now());
                    info.setModifier(JwtUtils.getLoginUserId());
                    info.setModifierName(JwtUtils.getLoginUsername());
                });
                boolean b1 = finishedOrderDetailService.updateBatchById(detailList);
                if (b1){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 确认提交
     * @param id
     * @return
     */
    @Override
    public boolean submit(Long id) {
        FinishedOrder byId = this.getById(id);
        byId.setSelStatus(ProductEnum.finishendOrderStatusEmum.DSH.getCode());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        byId.setUpdateTime(LocalDateTime.now());
        return updateById(byId);
    }

    /**
     * 审核
     * @param id
     * @return
     */
    @Override
    public boolean audit(Long id) {
        FinishedOrder byId = this.getById(id);
        byId.setSelStatus(ProductEnum.finishendOrderStatusEmum.YSL.getCode());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        byId.setUpdateTime(LocalDateTime.now());
        //TODU
        return updateById(byId);
    }
}
